<!-- 今日加速流量趋势 -->
<template>
  <div>
      <!-- <span class="title" v-show="Action !== '2'">最近一小时流量趋势</span> -->
      <div class="timer" v-show="Action !== '2'">
        <!-- <p>当前选择终端: {{name}}</p> -->
        自动刷新间隔<el-select style="margin:0 10px; width:100px;color:#2a2d3c;" v-model="timer" @change="setTimer()" :popper-class="color === 'night' ? 'nightClass' : ''">
          <el-option label='15秒' :value="15"></el-option>
          <el-option label='30秒' :value="30"></el-option>
          <el-option label='1分钟' :value="60"></el-option>
          </el-select>秒
      </div>
      <div ref="linechart" :class="className" :style="{height:height,width:width}"/>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { formatBytes } from "@/utils/index.js";
require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    Action: {
      type: String,
      default: '1'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    name: {
      type: String,
      default: '0.0.0.0'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      timer: 15,
      color: ''
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.color = localStorage.getItem('isNightMode') || 'night'
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setTimer(){
      this.$emit('setTimer', this.timer)
    },
    initChart() {
      this.chart = echarts.init(this.$refs.linechart, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ time, UpByte, downByte } = {}) {
      this.chart.setOption({
        xAxis: {
          data: time,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          // formatter:'{b}</br>{a}: {c} KB/S</br>{a1}: {c1} KB/S',
          formatter: function name(params) {
            return `${params[1].name}<br/>${params[1].marker}上行：${(params[1].value / 1024).toFixed(2)}KB/S<br/>${params[0].marker}下载：${(params[0].value / 1024).toFixed(2)}KB/S`
            // return `${params[1].name}<br/>${params[1].marker}上行：${formatBytes(params[1].value)}/S<br/>${params[0].marker}下载：${formatBytes(params[0].value)}/S`
          },
          padding: [5, 10]
        },
        yAxis: {
          axisLabel: {
            formatter: function(value) {
              return (value / 1024).toFixed(2) + ' KB';
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: "#666", // 修改网格线颜色为浅灰色
              type: "dashed", // 添加虚线效果
              width: 1 // 线条宽度
            }
          }
        },
        legend: {
          data: ['上行', '下载', '单位(KB/S)'],
          selectedMode: false,
          x:'right',
          textStyle: {
            color: this.color === "night" ? "#fff" : "#000" // 深色模式用白色，浅色模式用黑色
          }
        },
        title:{
          text:'最近流量趋势',
          right:'240px',
          y:'top',
          textAlign:'left',
          textStyle:{
            // color:'#aaa',
            color:'#459ffc',
            fontSize: 14
          }
        },
        series: [
          {
            name: '下载',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {}
              }
            },
            symbol: "none",
            data: downByte,
            animationDuration: 2800,
            animationEasing: 'quadraticOut',
            areaStyle:{}
          },
          {
            name:'单位(KB/S)',
            smooth: true,
            type: 'bar'
          },
          {
          name: '上行', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          symbol: "none",
          smooth: true,
          type: 'line',
          data: UpByte,
          animationDuration: 2800,
          animationEasing: 'cubicInOut',
          areaStyle:{}
        }
      ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.timer{
  font-size: 14px;
  color: #333333;
  margin-left: 20px;
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  p{
    margin-right: 10px;
  }
  .el-input{
    width: 80px;
    margin: 0 10px;
  }
}

.night{
  .timer{
    color: antiquewhite !important;
  }
  
}
@import "~@/styles/night.scss";

</style>



